<?php
/**
 * 房卡（钻石）计算和扣除
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/8/17
 * Time: 17:16
 */
class RoomDiamonds extends DatabaseBase
{
    /**
     * 计算要消耗的房卡总数（钻石）
     * @param $totalRound
     * @return float
     */
    public function calcTotalDiamonds($gameType, $totalRound)
    {
        $share = new ShareConfigInfo();
        $config = $share->get('required_diamonds');
        $config = json_decode($config, true);
        $totalDiamonds = 0;

        if ($config && isset($config[$gameType])) {
            foreach ($config[$gameType] as $key => $row) {
                if (count($row) == 2 && $row[0] == $totalRound) {
                    $totalDiamonds = $row[1];
                    break;
                }
            }
        } else {
            $this->returnError('invalid_game_type');
        }

        if (empty($totalDiamonds)) {
            $this->returnError('invalid_total_round');
        }

        return $totalDiamonds;
    }

    /**
     * 检测房卡是否足够（钻石）
     * @param $userInfo
     * @param $totalDiamonds
     * @return mixed
     */
    public function checkDiamond($userInfo, $totalDiamonds)
    {
        $diamond = (integer)$userInfo['freeDiamond'] + (integer)$userInfo['paidDiamond'];

        if ($diamond < $totalDiamonds) {
            $this->returnError('no_enough_diamond', -2, array('required' => $totalDiamonds));
        }
    }

    /**
     * 计算免费和付费房卡分别需要消费多少
     * @param $userDiamonds
     * @param $totalDiamonds
     */
    public function calcDiamond($userDiamonds, $totalDiamonds)
    {
        if ($userDiamonds['freeDiamond'] > $totalDiamonds) {
            $diamonds = array(
                'freeDiamond' => $totalDiamonds,
                'paidDiamond' => 0
            );
        } else {
            $diamonds = array(
                'freeDiamond' => $userDiamonds['freeDiamond'],
                'paidDiamond' => $totalDiamonds - $userDiamonds['freeDiamond'],
            );
        }

        return $diamonds;
    }
}